package com.mm.controller;

import com.mm.bo.EmployeeInsertBo;
import com.mm.bo.EmployeeSearchBo;
import com.mm.bo.EmployeeUpdateBo;
import com.mm.exception.CustomException;
import com.mm.service.EmployeeInfoService;
import com.mm.util.GetHeadUtil;
import com.mm.util.PageUtil;
import com.mm.util.ResultUtil;
import com.mm.vo.EmployeeInfoDetailsVo;
import com.mm.vo.EmployeeInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @ClassName EmpController
 * @Description TODO
 * @Author 任昊
 * @Date 2020/12/4 9:31
 */
@RestController
@Api(tags = "用于员工信息的接口")
public class EmployeeInfoController {

    @Resource
    private EmployeeInfoService employeeService;

    @ApiOperation(value = "增加员工", notes = "增加员工")
    @PostMapping("/v1/employee/insertEmployee")
    public ResultUtil insertEmployee(@RequestBody @Valid EmployeeInsertBo employeeInsertBo) {
        employeeInsertBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = employeeService.insertEmoloyeeInfo(employeeInsertBo);
        if (row < 1) {
            throw new CustomException("新增失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "查询员工列表(在职)", notes = "查询条件(工号or姓名、入职时间)")
    @PostMapping("/v1/employee/selectEmployee")
    public ResultUtil<Object> selectEmployee(@RequestBody @Valid EmployeeSearchBo employeeSearchBo) {
        employeeSearchBo.setEmpWorkStatus((long) 1);
        PageUtil<EmployeeInfoVo> pageUtil = employeeService.selectEmployeeInfo(employeeSearchBo);
        return ResultUtil.success(pageUtil);
    }

    @ApiOperation(value = "查询员工列表（离职）", notes = "查询条件(工号or姓名、入职时间)")
    @PostMapping("/v1/employee/selectEmployeenot")
    public ResultUtil<Object> selectEmployeenot(@RequestBody @Valid EmployeeSearchBo employeeSearchBo) {
        employeeSearchBo.setEmpWorkStatus((long) 0);
        PageUtil<EmployeeInfoVo> pageUtil = employeeService.selectEmployeeInfo(employeeSearchBo);
        return ResultUtil.success(pageUtil);
    }

    @ApiOperation(value = "查询员工详情", notes = "查询员工详情")
    @GetMapping("/v1/employee/selectEmployeeDetails/{empId}")
    public ResultUtil<Object> selectEmployeeDetails(@PathVariable("empId") String empId) {
        EmployeeInfoDetailsVo employeeInfoDetailsVo = employeeService.selectEmployeeInfoDetails(empId);
        return ResultUtil.success(employeeInfoDetailsVo);
    }

    @ApiOperation(value = "修改员工", notes = "修改员工信息")
    @PostMapping("/v1/employee/updateEmployee")
    public ResultUtil updateEmployee(@RequestBody @Valid EmployeeUpdateBo employeeUpdateBo) {
        employeeUpdateBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = employeeService.updateEmoloyeeInfo(employeeUpdateBo);
        if (row < 1) {
            throw new CustomException("修改失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "员工离职", notes = "员工离职")
    @ApiImplicitParam(value = "员工id", name = "empId", required = true)
    @GetMapping("/v1/employee/dimissionEmployee/{empId}")
    public ResultUtil dimissionEmployee(@PathVariable("empId") String empId) {
        Integer row = employeeService.dimissionEmoloyeeInfo(empId, GetHeadUtil.getHeadToken());
        if (row < 1) {
            throw new CustomException("离职失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "删除员工", notes = "删除员工信息")
    @ApiImplicitParam(value = "员工id(大于1个传数组 [  ])", name = "empId", required = true)
    @PostMapping("/v1/employee/deleteEmployee/{empId}")
    public ResultUtil deleteEmployee(@PathVariable("empId") String empId) {
        Integer row = employeeService.deleteEmoloyeeInfo(empId, GetHeadUtil.getHeadToken());
        if (row < 1) {
            throw new CustomException("删除失败");
        }
        return ResultUtil.success(null);
    }


}
